import logging
import aetycoon as dbExt
from google.appengine.ext import db
from application.models import Alliance

class Region(db.Model):
	name = db.StringProperty(required=True)
	searchableName = dbExt.LowerCaseProperty(name)
	updated = db.DateTimeProperty(required=True, auto_now=True)
	created = db.DateTimeProperty(required=True, auto_now_add=True)
	
	@classmethod
	def getByEveId(cls, eveId):
		return Region.get_by_key_name(str(eveId))
	
	@classmethod
	def updateOrAddFromJson(cls, json):
		eveId = str(json['regionID'])
		name = json['regionName']
		
		changed = False
		region = Region.get_or_insert(eveId,
					name=name)
		if name != region.name:
			region.name = name
			changed = True
		return (region, changed)
	
class Constellation(db.Model):
	name = db.StringProperty(required=True)
	searchableName = dbExt.LowerCaseProperty(name)
	region = db.ReferenceProperty(required=True, reference_class=Region, collection_name='constellations')
	updated = db.DateTimeProperty(required=True, auto_now=True)
	created = db.DateTimeProperty(required=True, auto_now_add=True)
		
	@classmethod
	def getByEveId(cls, eveId):
		return Constellation.get_by_key_name(str(eveId))
		
	@classmethod
	def updateOrAddFromJson(cls, json):
		eveId = str(json['constellationID'])
		name = str(json['constellationName'])
		region = Region.getByEveId(json['regionID'])

		changed = False
		constellation = Constellation.get_or_insert(eveId,
							name=name,
							region=region)
		if name != constellation.name:
			constellation.name = name
			changed = True
		if region.key().id_or_name() != constellation.region.key().id_or_name():
			constellation.region = region
			changed = True
		return (constellation, changed)
		
class SolarSystem(db.Model):
	name = db.StringProperty(required=True)
	searchableName = dbExt.LowerCaseProperty(name)
	constellation = db.ReferenceProperty(reference_class=Constellation, collection_name='solarSystems')
	security = db.FloatProperty()
	sovereignty = db.ReferenceProperty(reference_class=Alliance, collection_name='solarSystems') 
	updated = db.DateTimeProperty(required=True, auto_now=True)
	created = db.DateTimeProperty(required=True, auto_now_add=True)

	@classmethod
	def getByEveId(cls, eveId):
		return SolarSystem.get_by_key_name(str(eveId))

	@classmethod
	def updateOrAddFromJson(cls, json):
		eveId = str(json['solarSystemID'])
		name = str(json['solarSystemName'])
		constellation = Constellation.getByEveId(json['constellationID'])
		security = float(json['security'])

		changed = False
			
		solarSystem = SolarSystem.get_or_insert(eveId,
							name=name,
							constellation=constellation,
							security=security)
		if name != solarSystem.name:
			solarSystem.name = name
			changed = True
		if constellation.key().id_or_name() != solarSystem.constellation.key().id_or_name():
			solarSystem.region = constellation
			changed = True
		if solarSystem.security != security:
			solarSystem.security = security
			changed = True
		return (solarSystem, changed)
			
	@classmethod
	def updateOrAddFromSovApi(cls, record):
		eveId = str(record.solarSystemID)
		name = str(record.solarSystemName)
		try:
			alliance = Alliance.getByEveId(record.allianceID)
		except db.BadKeyError:
			alliance = None
		
		changed = False

		solarSystem = SolarSystem.get_or_insert(eveId,
							name=name,
							sovereignty=alliance)
		if name != solarSystem.name:
			solarSystem.name = name
			changed = True
		if alliance is None:
			if solarSystem.sovereignty is not None:
				solarSystem.sovereignty = None
				changed = True
		else:
			if solarSystem.sovereignty is None or solarSystem.sovereignty.key().id_or_name() != alliance.key().id_or_name():
				solarSystem.sovereignty = alliance
				changed = True
		return (solarSystem, changed)

class Moon(db.Model):
	name = db.StringProperty(required=True)
	searchableName = dbExt.LowerCaseProperty(name)
	solarSystem = db.ReferenceProperty(required=True, reference_class=SolarSystem) 
	
	@classmethod
	def getByEveId(clscls, eveId):
		return Moon.get_by_key_name(str(eveId))
		
	@classmethod
	def updateOrAddFromJson(cls, json):
		eveId = str(json['itemID'])
		name = str(json['itemName'])
		solarSystem = SolarSystem.getByEveId(json['solarSystemID'])

		changed = False
		moon = Moon.get_or_insert(eveId,
				name=name,
				solarSystem=solarSystem)
		
		if name != moon.name:
			moon.name = name
			changed = True
		if solarSystem.key().id_or_name() != moon.solarSystem.key().id_or_name():
			moon.solarSystem = solarSystem
			changed = True
		return (moon, changed)
			
